/**
 * Copyright (c) 2023, iDearHanYu Co., Ltd.
 *
 * SPDX-License-Identifier: 
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-09-21     chao         first version
 */

#include <lvgl.h>
#include "volumio_music.h"

struct _page_item {
    uint8_t page_id;
    lv_obj_t *(*create)(void);
    void (*exit)(void);
};

struct _page {
    uint8_t current_page_id;
    lv_obj_t *current_page;
    uint8_t next_page_id;
    lv_obj_t *next_page;
    const struct _page_item *page_item;
};

static const struct _page_item page_items[PAGE_ID_MAX] = {
    {PAGE_ID_STARTUP, volumio_music_startup_create, volumio_music_startup_destroy},
    {PAGE_ID_PLAYER, volumio_music_player_create, volumio_music_player_destroy},
    {PAGE_ID_SOURCE, volumio_music_source_create, volumio_music_source_destroy},
    {0},
};
static struct _page page;

void volumio_music_page_init(void)
{
    page.current_page = NULL;
    page.current_page_id = PAGE_ID_MAX;
    page.next_page = NULL;
    page.next_page_id = PAGE_ID_MAX;
    page.page_item = NULL;
}

lv_obj_t *volumio_music_current_page_get(void)
{
    return page.current_page;
}

uint8_t volumio_music_current_page_id_get(void)
{
    return page.current_page_id;
}

void volumio_music_page_switch(uint8_t page_id)
{
    if ((page_id==page.current_page_id) || (page_id>=PAGE_ID_MAX)) return;
    page.next_page_id = page_id;
    if (page.current_page_id<PAGE_ID_MAX && page.page_item!=NULL)
    {
        page.page_item->exit();
    }
    page.next_page = page_items[page.next_page_id].create();
    page.current_page_id = page.next_page_id;
    page.current_page = page.next_page;
    page.page_item = &page_items[page.next_page_id];
}


